<template>
  <div>
    <eagle-dialog-page
      :url="url"
      :params="fileParams"
      @initList="_initList"
      @open="open"
      @submit="submit"
      @callbackDialog="callbackDialog"
      @setData="setData"
      v-bind="$attrs"
      v-on="$listeners"
      ref="eagleDialogPage"
      :option="option"
      :selectedValue="selectedValue"
    >
      <van-cell v-for="(item, index) in list" :key="index">
        <div class="risk_item_radio">
          <div class="mb-5">
            <div class="mb-10">{{ item.FileName }}</div>
            <div class="mb-10">文件类型：{{ item.PlanType }}</div>
            <div>
              <eagle-file-list
                :attachs="item.IssueMainAttach"
                :labelName="'签发版本:'"
              ></eagle-file-list>
            </div>
          </div>
          <eagle-checkbox
            v-if="$attrs.isMulti ? $attrs.isMulti : false"
            :name="item.FileManageCode"
            shape="round"
          />
          <van-radio
            v-else
            :name="item.FileManageCode"
            @click="radioClick(item)"
          ></van-radio>
        </div>
      </van-cell>

      <template #seletedSlot>
        <van-cell
          name="seletedSlot"
          v-for="(item, index) in multiselectedlist"
          :key="index"
        >
          <div class="risk_item_buton">
            <div class="mb-5">
              <div class="mb-10">{{ item.FileName }}</div>
              <div>
                <eagle-file-list
                  :attachs="item.IssueMainAttach"
                  :labelName="'签发版本:'"
                ></eagle-file-list>
              </div>
            </div>
            <van-icon name="delete" @click="deleteSelect(item.OrgCode)" />
          </div>
        </van-cell>
      </template>
    </eagle-dialog-page>
  </div>
</template>
<script>
export default {
  name: "plan-emergencyfile-dialog",
  props: {
    selectedValue: {
      type: String,
      default: () => "",
    },
    tagData: {
      type: [Array, String],
      default: () => [],
    },
  },
  data() {
    return {
      url: this.$constUrl.PlanEmergencyFileManage_GetPageData,
      list: [],
      fileParams: [
        {
          Name: "FileName",
          dataType: "Window",
          orgCode: "-1",
        },
      ],
      selectedData: [],
      selecteCode: [],
      result: "",
      option: { name: "FileName", code: "FileManageCode" },
      isInit: true,
      multiselectedlist: [],
      orgRowIndex: -1,
      simSelectObject: { FileManageCode: "", fileName: "" },
    };
  },
  computed: {
    multi: {
      get() {
        if (this.$attrs.isMulti) {
          return this.$attrs.isMulti;
        } else {
          return false;
        }
      },
    },
  },
  methods: {
    _initList(list) {
      if (
        this.$attrs.isOpenNew ||
        (this.$attrs.rowIndex && this.$attrs.rowIndex != this.orgRowIndex)
      ) {
        this.orgRowIndex = this.$attrs.rowIndex;
        this.multiselectedlist = [];
      }
      this.list = list;
      this.list.forEach((element) => {
        if (element.PlanType == "ZHYJYA") {
          element.PlanType = "综合应急预案";
        } else if (element.PlanType == "XCCZFA") {
          element.PlanType = "现场处置方案";
        } else if ((element.PlanType = "ZXYJYA")) {
          element.PlanType = "专项应急预案";
        }
      });
      this.selecteCode = [];
      var newData = [];
      if (!this.multi) {
        newData = [
          { code: this.selectedValue, FileManageCode: this.selectedValue },
        ];
      } else {
        for (var i = 0; i < this.tagData.length; i++) {
          var innerItem = this.tagData[i];
          this.selecteCode.push(innerItem.PlanType);
          newData.push({
            FileManageCode: innerItem.PlanType,
            FileName: innerItem.FileName,
            code: innerItem.PlanType,
            name: innerItem.FileName,
          });
        }
      }
      this.$refs.eagleDialogPage.initSeletedData(newData);
      this.multiselectedlist = newData;
    },
    open(list) {
      this.list = list;
    },
    submit() {
      this.$refs.eagleDialogPage.submit();
    },
    setData(data) {
      if (this.multi) {
        this.multiselectedlist = data;
      } else {
        this.simSelectObject = data;
      }
    },
    deleteSelect(code) {
      this.$refs.eagleDialogPage.deleteSelect(code);
    },
    radioClick(item) {
      let resultRadio = Object.assign(
        { planType: "", fileName: "", fileCode: "" },
        item
      );
      resultRadio.planType = item.PlanType;
      resultRadio.fileName = item.FileName;
      resultRadio.fileCode = item.FileManageCode;
      this.$emit("callback", resultRadio);
      this.$emit("update:show", false);
    },
    callbackDialog(data) {
      this.$emit("callback", {
        planType: data.PlanType,
        fileName: data.FileName,
        fileCode: data.FileManageCode,
      });
    },
  },
};
</script>
<style lang="less" scoped>
.risk_item_radio {
  width: 100%;
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
